package cn.lili.controller.promotion;

import cn.lili.common.enums.CodeTypeEnum;
import cn.lili.common.enums.ResultCode;
import cn.lili.common.security.AuthUser;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.utils.CodeUtil;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.PageVO;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.promotion.entity.dos.BlindboxRule;
import cn.lili.modules.promotion.entity.dos.LiBlindboxRuleGoods;
import cn.lili.modules.promotion.entity.dto.search.CouponSearchParams;
import cn.lili.modules.promotion.entity.vos.BlindboxRuleVO;
import cn.lili.modules.promotion.entity.vos.CouponVO;
import cn.lili.modules.promotion.service.BlindboxRuleService;
import cn.lili.modules.promotion.service.LiBlindboxRuleGoodsService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import io.swagger.annotations.Api;
import org.apache.shardingsphere.transaction.annotation.ShardingTransactionType;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * @author xiangqian
 */
@RestController
@Api(tags = "盲盒规则接口")
@RequestMapping("/promotion/blindbox/rule")
@RequiredArgsConstructor
public class BlindboxRuleController {

    private final BlindboxRuleService blindboxRuleService;
    private final LiBlindboxRuleGoodsService liBlindboxRuleGoodsService;

    @ApiOperation(value = "添加盲盒规则")
    @PostMapping
    @Transactional
    @ShardingTransactionType(TransactionType.BASE)
    public ResultMessage<BlindboxRuleVO> save(@RequestBody @Valid BlindboxRuleVO blindboxRuleVO) {
        //vo转dos
        BlindboxRule blindboxRule = new BlindboxRule(blindboxRuleVO);
        blindboxRule.setRuleCode(CodeUtil.generateNumber(CodeTypeEnum.RULE.clientName()));
        //校验规则奖品是否与规则总次数一致
        Integer allGoodsNumber = blindboxRuleVO.getGoods().stream().mapToInt(LiBlindboxRuleGoods::getRuleGoodsNumber).sum();
        Integer allRuleNumber = blindboxRuleVO.getRuleNum() * blindboxRuleVO.getRuleBuyNum();
        if(!allGoodsNumber.equals(allRuleNumber)){
            return ResultUtil.error(HttpStatus.INTERNAL_SERVER_ERROR.value(),"需要：总次数 * 购买次数 = 奖品数量");
        }

        //新增规则
        blindboxRuleVO.setRuleBuyNum(1);
        blindboxRuleService.save(blindboxRule);
        //新增规则图片
        List<LiBlindboxRuleGoods> goods = blindboxRuleVO.getGoods();
        for (LiBlindboxRuleGoods liBlindboxRuleGoods:goods) {
            liBlindboxRuleGoods.setRuleId(blindboxRule.getId());
            liBlindboxRuleGoods.setRuleGoodsOffNumber(0);
            liBlindboxRuleGoodsService.save(liBlindboxRuleGoods);
        }
        return ResultUtil.data(blindboxRuleVO);
    }

    @ApiOperation(value = "获取盲盒规则列表")
    @GetMapping
    public ResultMessage<Page<BlindboxRuleVO>> getBlindboxRuleList(BlindboxRuleVO queryParam, PageVO page) {
        return ResultUtil.data(blindboxRuleService.getBlindboxRuleList(queryParam, page));
    }

    @ApiOperation(value = "根据规则ID查询规则奖品")
    @GetMapping("/getRuleGoods")
    public ResultMessage<List<LiBlindboxRuleGoods>> getRuleGoods(String ruleId) {
        return ResultUtil.data(liBlindboxRuleGoodsService.getLiBlindboxRuleGoodsByRuleId(ruleId));
    }
}
